<?php

namespace App\Admin\Controllers\User;

use App\Admin\Repositories\User\WxUserInviteBenefitRecord;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxUserInviteBenefitRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxUserInviteBenefitRecord(), function (Grid $grid) {
            $grid->quickSearch(['id', 'user_id', 'order_id', 'shop_order_id', 'lv1_user','lv2_user', 'lv3_user'])->placeholder('搜索id，用户id，订单id，商城订单id，一级用户id，二级用户id，三级用户id...')->width(100);
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('user_id')->sortable();
            $grid->column('order_id')->sortable()->display(function ($v){
                if($v > 0){
                    return '<a href="'.admin_url('order?id='.$v).'" target="_blank">普通订单'.$v.'</a>';
                }
                return '';
            });
            $grid->column('shop_order_id')->sortable()->display(function ($v){
                if($v > 0){
                    return '<a href="'.admin_url('shop/order?id='.$v).'" target="_blank">商品订单'.$v.'</a>';
                }
                return '';
            });
            $grid->column('order_price')->sortable();
            $grid->column('lv1_user')->sortable();
            $grid->column('lv1_benefit')->sortable();
            $grid->column('lv2_user')->sortable();
            $grid->column('lv2_benefit')->sortable();
            $grid->column('lv3_user')->sortable();
            $grid->column('lv3_benefit')->sortable();
            $grid->column('created_at')->sortable();
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user_id');
                $filter->equal('lv1_user');
                $filter->equal('lv2_user');
                $filter->equal('lv3_user');
            });
            $grid->disableActions();
            $grid->disableActions();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxUserInviteBenefitRecord(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('order_id');
            $show->field('shop_order_id');
            $show->field('order_price');
            $show->field('lv1_user');
            $show->field('lv1_benefit');
            $show->field('lv2_user');
            $show->field('lv2_benefit');
            $show->field('lv3_user');
            $show->field('lv3_benefit');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxUserInviteBenefitRecord(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('order_id');
            $form->text('shop_order_id');
            $form->text('order_price');
            $form->text('lv1_user');
            $form->text('lv1_benefit');
            $form->text('lv2_user');
            $form->text('lv2_benefit');
            $form->text('lv3_user');
            $form->text('lv3_benefit');

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
